<? require("adm_header.php"); ?>

<? writeCommonAdminTable(LA_ADMIN_BANNER_ADMIN,1); ?>

<?
include "language_src/eng_system.inc";

$upload_ok=0;
$cid=getParam("cid","");
$bid=getParam("bid","");
$size=getParam("size","468x60");
$status=getParam("status",1);
$html_banner=$_REQUEST["html_banner"];
$run_for_hits=getParam("run_for_hits",900000);
$run_for_days=getParam("run_for_days",900000);
$section=getParam("section","banner_section1");

$run_for_clicks=getParam("run_for_clicks",900000);
$goto_url=getParam("goto_url","");
if ($_REQUEST["run_in_group"])
	$run_in_group=$_REQUEST["run_in_group"];
$edit_banner=getParam("edit_banner","");
$tplUpload = new tplLoad();
$tplUpload->assign("cid",$cid);
$tplUpload->assign("bid",$bid);
$tplUpload->assign("size","$size");
$tplUpload->assign("section","$section");
$tplUpload->assign("edit_banner",$edit_banner);
$sql="select * from $adv_tbl where id=$cid limit 1";
$adw_row=mysql_fetch_array(q($sql));
$name=$adw_row["name"];
$tplUpload->assign("banner_upload_descr",formatString(LA_ADMIN_BANNER_UPLOAD,array("$cid","$name")));

$tplUpload->assign("name",$name);
$size = floor($piclimit/1024);
if (getParam("del","") && !$set_demo)
{
	$sql="select filename from $banner_tbl where bid=" . getParam("del","");
	$r=q($sql);
	$row=mysql_fetch_array($r);
	$del_file=$row["filename"];
	if (unlink("$set_path/images/banners/$del_file"))
	{
		q("delete from $banner_tbl where bid=" . getParam("del",""));
		$tplUpload->assign("banner_notice","Banner <b>$del_file</b> was deleted OK");
	}
	else 
		$tplUpload->assign("banner_notice","<font color='red'>Error</font><br>Banner <b>$del_file</b> could either not be found or your filepermissions on this file was wrong. Change CHMOD settings on the banner file, and push Refresh in browser. The banner will appear in banner table until a successfull delete is done on that file.");
		
	
	$tplUpload->assign("show_form",1);
}



if (isset($_REQUEST["submit"]) AND !$stop AND !$set_demo)
{
	
	$FileArray=$_FILES["photo"];
	$SaveAs=$set_path . "/images/banners/$cid";
	$MaxSize="$piclimit";
	
	
	$DimensionArray[]=getParam("size","") . "!";
	
	$lRunInGroup="";
	if ($run_in_group)
	{
	foreach ($run_in_group as $group_id)
		$lRunInGroup.=":$group_id:";
	}
	if ((getParam("replace_banner","")==1 AND $edit_banner) || !$edit_banner)
	{
		$returnValue=uploadImage($FileArray,$SaveAs,$set_image_limit*1024,$DimensionArray,"",getParam("program",0));
	
		if (is_int($returnValue))
			$tplUpload->assign("error",getErrorMsg($returnValue));
		else 
		{
			foreach ($returnValue as $item)
			{
				//echo "Filen $item ble lastet opp!<br>";
				//echo "<img src='../images/banners/$item'><br>";
				@chmod("../images/banners/$item",octdec($set_chmod));
				
				
			}
			if (!$edit_banner)
			{
				$sq = "INSERT INTO $banner_tbl (cid,filename,run_for_hits,";
				$sq.="run_for_clicks,run_for_days,run_in_group,goto_url,status,html_banner,section) VALUES ($cid,'$item'";
				$sq.=",$run_for_hits,$run_for_clicks,$run_for_days,'$lRunInGroup','$goto_url',$status,'".$html_banner."','$section')";
				$result=q($sq);
				$id = mysql_insert_id();
				$upload_ok=1;
				$tplUpload->assign("upload_ok",1);
			}
			$upload_ok=1;
			$tplUpload->assign("show_form","");
		}
	
	
	}
	
	if ($edit_banner)
	{
		$sq = "update $banner_tbl set ";
		if ($upload_ok)
			$sq.="filename='$item',";
		$sq.="run_for_hits=$run_for_hits,";
		$sq.="run_for_clicks=$run_for_clicks,";
		$sq.="run_for_days=$run_for_days,";
		$sq.="html_banner='".$html_banner."',";
		$sq.="goto_url='$goto_url',";
		$sq.="section='$section',";
		$sq.="status=$status,";
		$sq.="run_in_group='$lRunInGroup'";
		$sq.=" where bid=$edit_banner";
		$result=q($sq);
	
		$tplUpload->assign("upload_ok",1);
		$tplUpload->assign("show_form","");
	}
	
	

}
// If less than maxpic has been uploaded, show upload-form
if (!$stop AND $upload_ok!=1)
{
	$tplUpload->assign("show_form",1);
}

if ($cid)
{
	if ($edit_banner)
	{
		$query = "select * from $banner_tbl where bid=$edit_banner";
		$sql_result = q($query);
		$i=0;
		$row_edit=mysql_fetch_array($sql_result);
		$lFileName=$row_edit["filename"];
		$tplUpload->assign("edit_banner",$edit_banner);
		$tplUpload->assign("image",$lFileName);
		$run_for_clicks=$row_edit["run_for_clicks"];
		$run_for_days=$row_edit["run_for_days"];
		$run_for_hits=$row_edit["run_for_hits"];
		$html_banner=$row_edit["html_banner"];
		$lRunInGroup=$row_edit["run_in_group"];
		$lGoto_url=$row_edit["goto_url"];
		$section=$row_edit["section"];
		$lStatus=$row_edit["status"];
		$banner=$row_edit["run_in_group"];
		if ($run_in_group)
		{
			foreach ($run_in_group as $group_id)
				$lRunInGroup.=":$group_id:";
		}
		
	}
	$status=getParam("status","$lStatus");
	if (!$status)
		$status=1;
	
	$tplUpload->assign("run_for_days",getParam("run_for_days",$run_for_days));
	$tplUpload->assign("run_for_clicks",getParam("run_for_clicks",$run_for_clicks));
	$tplUpload->assign("run_for_hits",getParam("run_for_hits",$run_for_hits));
	$tplUpload->assign("goto_url","$lGoto_url");
	$tplUpload->assign("html_banner","$html_banner");
	$tplUpload->assign("status",$status);
	$tplUpload->assign("section",$section);
	$tplUpload->assign("name",$name);
	$lCheckedArray=split(":",$lRunInGroup);
	$list=getCategoriesDD($lCheckedArray,"");	
	$tplUpload->assign("list",$list);

	
	$query = "select * from $banner_tbl where cid=$cid order by bid desc";
	$sql_result = q($query);
	$i=0;
	while ($row=mysql_fetch_array($sql_result))
	{
		
		$bid = $row["bid"];
		$cid = $row["cid"];
		$filename = $row["filename"];
	
		$fArray[$i]["id"] = $id;
		$fArray[$i]["status"] = $row["status"];
		//$fArray[$i]["html"] = getOneImage("",$id,"","");
		$fArray[$i]["filename"] = $filename;
		$fArray[$i]["bid"] = $bid;
		$fArray[$i]["cid"] = $cid;
		$i++;
	}
	$tplUpload->assign("image_list", $fArray);
}
$tplUpload->display("adm_banner.tpl");
writeCommonAdminTable("Payment Editor","");
include_once("adm_footer.php"); 
?>
